package com.xuxk.project.library.controller.api;

import com.github.pagehelper.Page;
import com.xuxk.project.library.annotation.LibraryLog;
import com.xuxk.project.library.bean.RoleMenu;
import com.xuxk.project.library.service.RoleMenuService;
import com.xuxk.project.library.util.PageInfoVo;
import com.xuxk.project.library.util.PageUtil;
import com.xuxk.project.library.util.RequestUtils;
import com.xuxk.project.library.util.ResultBodyVo;
import com.xuxk.project.library.vo.RoleMenuVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 *   @author  橙云科技有限公司
 */

@RequestMapping("/api/roleMenu")
@RestController
public class RoleMenuController {

    @Autowired
	RoleMenuService roleMenuService;


    /**
    * 分页查询
    * @param pageNum  页码
    * @param pageSize 页行数
    * @param request
    *
    * @return
    */
    @GetMapping(value = "/v1.0/roleMenus", produces = MediaType.APPLICATION_JSON_VALUE)
    @LibraryLog(eventName = "分页查询角色拥有菜单关系列表")
	public ResultBodyVo roleMenus(Integer pageNum, Integer pageSize, HttpServletRequest request){

        Map<String,Object> paramMap = RequestUtils.getQueryParams(request);
        Page<RoleMenuVo> pageResult = roleMenuService.listRoleMenus(paramMap, PageUtil.getPageParam(pageNum,pageSize));

		return new ResultBodyVo(new PageInfoVo(pageResult)) ;
	}



    @GetMapping(value="/v1.0/roleMenus/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
    @LibraryLog(eventName = "获取角色菜单对应关系")
    public ResultBodyVo getRoleMenu(@PathVariable Integer id) {
        RoleMenu roleMenu = roleMenuService.getRoleMenu(id);

        return new ResultBodyVo(roleMenu);
    }

    @PostMapping(value="/v1.0/roleMenus", produces = MediaType.APPLICATION_JSON_VALUE)
    public ResultBodyVo saveRoleMenu(@RequestBody RoleMenu roleMenu) {
        return  new ResultBodyVo(roleMenuService.insertRoleMenu(roleMenu));

    }


    @PatchMapping(value="/v1.0/roleMenus/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
    @LibraryLog(eventName = "部分修改角色菜单对应关系")
    public ResultBodyVo patchUpdateRoleMenu(@PathVariable Integer id,@RequestBody RoleMenu roleMenu) {
        roleMenu.setId(id);
        return  new ResultBodyVo(roleMenuService.patchUpdateRoleMenu(roleMenu));


    }

    @PutMapping(value="/v1.0/roleMenus/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
    @LibraryLog(eventName = "全量更新角色菜单关系")
    public ResultBodyVo updateRoleMenu(@PathVariable Integer id,@RequestBody RoleMenu roleMenu) {

        roleMenu.setId(id);
        return new ResultBodyVo(roleMenuService.updateRoleMenu(roleMenu));
    }


    @DeleteMapping(value="/v1.0/roleMenus/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
    @LibraryLog(eventName = "删除角色菜单关系")
    public ResultBodyVo deleteRoleMenu(@PathVariable Integer id) {

          roleMenuService.deleteRoleMenu(id );

        return new ResultBodyVo("删除成功！") ;

    }

}
